package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
			  ________   ___   ___        ___   ________   ___   ___        ___
	 ________|\   __  \_|\  \_|\  \______|\  \_|\   __  \_|\  \_|\  \______|\  \_________
	 \       \ \  \_\ /_\ \  \\ \  \     \ \  \\ \  \_\ /_\ \  \\ \  \     \ \  \        \
	  \       \ \   __  \\ \  \\ \  \     \ \  \\ \   __  \\ \  \\ \  \     \ \  \        \
	   \       \ \  \_\  \\ \  \\ \  \____ \ \  \\ \  \_\  \\ \  \\ \  \____ \ \  \        \
	    \       \ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\        \
	     \       \|_______| \|__| \|_______| \|__| \|_______| \|__| \|_______| \|__|         \
	 	  \___________________________________________________________________________________\

+------------+
|2020/4/6   |
|14:11       |
+------------+
*/
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao groupDao;
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        groupDao.add(checkGroup);
        //根据回显的id进行添加
        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        //pageHelp查询
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = groupDao.selectByCondition(queryString);
        //封装查询结果和总记录数
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public CheckGroup findById(Integer id) {

        return groupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return groupDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //编辑检查组信息
        groupDao.edit(checkGroup);
        //编辑检查项
        //先删除所有关联信息,再建立新的联系
        groupDao.deleteAssociation(checkGroup);

        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
    }

    @Override
    public void delete(Integer id) {
        //删除关系表中关系
        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setId(id);
        groupDao.deleteAssociation(checkGroup);
        //再删除检查组
        groupDao.deleteGroup(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return groupDao.findAll();
    }

    public void setCheckGroupAndCheckItem(int checkGroupId,Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkGroup_id", checkGroupId);
                map.put("checkItem_id", checkitemId);
                groupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }
}
